package org.itboys.mobile.controller.mobile.member;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.dto.member.SundayMemberDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.mobile.member.SundayMobileMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/11/30.
 * @author 朱坤涛
 * 会员App接口
 */
@Controller
@RequestMapping("/mdl/mobile/member")
@Api(value = "member",description = "用户")
public class MdlMobileMemberController extends BaseController{

    @Autowired
    private SundayMobileMemberService memberService;


    /** 【用户版】用户注册。手机号和密码
     * @param response
     * @param mobile
     * @param password
     * @param code
     */

    @RequestMapping(value="/register", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】用户注册。手机号和密码。-1直接提示消息")
    public void register(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true,name = "mobile",value = "手机号")@RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true,name = "password",value = "设置密码")@RequestParam(value = "password", required = true) String password,
            @ApiParam(required = false,name = "recommendId",value = "推荐人Id")@RequestParam(value = "recommendId", required = false) Long recommendId,
            @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try {
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("mobile", mobile);
            param.put("password", password);
            param.put("recommendId", recommendId);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto member = memberService.register(mobile,password,code,recommendId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(member, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/register----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }

    }

    /**
     *代理商给销售员注册或修改账号
     * @param mobile
     * @param agentId
     * @param request
     * @param response
     */
   /* @RequestMapping(value="/registerSalesMan", method= RequestMethod.POST)
    @ApiOperation(value="【代理商管理版】注册销售员。手机号和密码。")
    public void registerSalesMan(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true,name = "mobile",value = "手机号")@RequestParam String mobile,
            @ApiParam(required = true,name = "agentId",value = "推荐人id  当前代理商id")@RequestParam Long agentId,
            @ApiParam(required = true,name = "code",value = "验证码")@RequestParam String code,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
            ){
        try {
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("mobile", mobile);
            param.put("agentId", agentId);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto sundayMember = memberService.registerSalesMan(mobile,agentId,code);
            AjaxUtils.renderJsonporJson(response,null,ResultPageDOHelper.getMsgResultDO(sundayMember,CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/registerSalesMan----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }*/

    /**
     * 代理商生成维修工和销售员
     * @param agentId
     * @param userName
     * @param password
     * @param nickName
     * @param mobile
     * @param request
     * @param response
     */
    @RequestMapping(value="/registerMember", method= RequestMethod.POST)
    @ApiOperation(value="【代理商管理版】注册维修工,销售员。手机号和密码。")
    public void registerSeller(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true,name = "agentId",value = "推荐人id  当前代理商id")@RequestParam Long agentId,
            @ApiParam(required = true,name = "userName",value = "账号")@RequestParam String userName,
            @ApiParam(required = true,name = "password",value = "密码")@RequestParam String password,
            @ApiParam(required = true,name = "nickName",value = "姓名（昵称）")@RequestParam String nickName,
            @ApiParam(required = true,name = "mobile",value = "手机号")@RequestParam String mobile,
            @ApiParam(required = true,name = "type",value = "用户类型 2，销售员  3，维修工")@RequestParam Integer type,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
            ){
        try {
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("agentId", agentId);
            param.put("userName", userName);
            param.put("password", password);
            param.put("nickName", nickName);
            param.put("mobile", mobile);
            param.put("type", type);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto sundayMember = memberService.registerMember(agentId,userName,password,nickName,mobile,type);
            AjaxUtils.renderJsonporJson(response,null,ResultPageDOHelper.getMsgResultDO(sundayMember,CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/registerMember----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 分页获取用户
     * @param request
     * @param response
     * @param userType
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/selectMember", method= RequestMethod.POST)
    @ApiOperation(value="获取用户列表。分页")
    public void selectMember(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = false, name = "agentId", value = "当前用户 即当前代理商Id") @RequestParam(value = "agentId", required = true) Long recommenderId,
            @ApiParam(required = false, name = "userType", value = "用户类型。2销售员。3维修工") @RequestParam(value = "userType", required = true) Integer userType,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。销量：saleNumber,价格：price") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            List<SundayMemberDto> result=memberService.selectMember(request);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/selectMember----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


    /**
     * 【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息
     * @param request
     * @param response
     * @param mobile
     * @param password
     */

    @RequestMapping(value="/login", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息")
    public void login(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                      @ApiParam(required=true, name="password", value="密码。明文")@RequestParam(value = "password", required = true)String password,
                      @ApiParam(required=true, name="loginType", value="登陆类型。1用户版，2管理版")@RequestParam(value = "loginType", required = true)Integer loginType,
                      @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                      @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("mobile", mobile);
            param.put("password", password);
            param.put("loginType", loginType);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto member= memberService.login( mobile,password,loginType);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/login----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }



  /**
   * 用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第第二步。
   * @param request
   * @param response
   * @param thirdType
   * @param unionId
   */
    @RequestMapping(value="/loginWithThirdOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第二步。")
    public void loginWithThirdOne(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                      @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                  @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("thirdType", thirdType);
            param.put("unionId", unionId);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto member= memberService.loginWithThirdOne(thirdType, unionId);
            if(member==null){
                ResponseMessage.error(-2,"需要验证手机号");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/loginWithThirdOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

/**
 * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
 * @param request
 * @param response
 * @param thirdType
 * @param nickName
 * @param image
 * @param unionId
 * @param mobile
 * @param code
 */

    @RequestMapping(value="/loginWithThirdTwo", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。")
    public void loginWithThirdTwo(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                                  @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                  @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("thirdType", thirdType);
            param.put("nickName", nickName);
            param.put("image", image);
            param.put("unionId", unionId);
            param.put("mobile", mobile);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto member= memberService.loginWithThirdTwo( thirdType, nickName, image,  unionId,  mobile, code);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/loginWithThirdTwo----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }


     /** 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     * @param password
     */

    @RequestMapping(value="/loginWithThirdThree", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第三步。（共三步）。-1直接提示消息；")
    public void loginWithThirdThree(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                                  @ApiParam(required=true, name="password", value="密码（明文）")@RequestParam(value = "code", required = true)String password,
                                    @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                    @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("thirdType", thirdType);
            param.put("nickName", nickName);
            param.put("image", image);
            param.put("unionId", unionId);
            param.put("mobile", mobile);
            param.put("code", code);
            param.put("password", password);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            SundayMemberDto member= memberService.loginWithThirdThree( thirdType, nickName, image,  unionId,  mobile, code, password);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/loginWithThirdThree----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版和管理版】退出登录。-1直接提示消息；
     * @param request
     * @param response
     */

    @RequestMapping(value="/logout", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】退出登录。-1直接提示消息；")
    public void logout(HttpServletRequest request,
                       HttpServletResponse response,
                       @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.logout(memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/logout----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【用户版和管理版】修改个人信息。-1直接提示消息；
     * @param request
     * @param response
     * @param nickName
     * @param mobileName
     * @param address
     * @param email
     * @param image
     * @param idCard
     * @param desc
     * @param nonce
     * @param sign
     */

    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改个人信息。-1直接提示消息；会返回新的token，记得更新移动端的token")
    public void update(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
                       @ApiParam(required=false, name="mobileName", value="联系人")@RequestParam(value = "mobileName", required = false)String mobileName,
                       @ApiParam(required=false, name="address", value="地址")@RequestParam(value = "address", required = false)String address,
                       @ApiParam(required=false, name="email", value="电子邮箱")@RequestParam(value = "email", required = false)String email,
                       @ApiParam(required=false, name="image", value="头像")@RequestParam(value = "image", required = false)String image,
                       @ApiParam(required=false, name="idCard", value="身份证号码")@RequestParam(value = "idCard", required = false)String idCard,
                       @ApiParam(required=false, name="desc", value="个人简介")@RequestParam(value = "desc", required = false)String desc,
                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nickName", nickName);
            param.put("mobileName", mobileName);
            param.put("address", address);
            param.put("email", email);
            param.put("image", image);
            param.put("idCard", idCard);
            param.put("desc", desc);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayMemberDto result =memberService.update( memberId, nickName,mobileName, address, email, image, idCard, desc);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/update----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


    /**
     * 用户版和管理版】忘记密码
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */

    @RequestMapping(value="/forgetPassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】忘记密码。。-1直接提示消息；")
    public void forgetPassword(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                               @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                               @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            memberService.forgetPassword(mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/forgetPassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 用户版和管理版】修改密码。使用验证码修改
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */

    @RequestMapping(value="/updatePasswordWithCode", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用验证码修改。-1直接提示消息；")
    public void updatePasswordWithCode(HttpServletRequest request,
                                       HttpServletResponse response,
                                       @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                       @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                                       @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                       @ApiParam(required = true,value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.updatePasswordWithCode(memberId,mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/updatePasswordWithCode----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }


    /**
     * 【用户版和管理版】修改密码。使用旧密码修改
     * @param request
     * @param response
     * @param oldPassword
     * @param newPassword
     */
    @RequestMapping(value="/updatePassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用旧密码修改。-1直接提示消息；")
    public void updatePassword(HttpServletRequest request,

                               HttpServletResponse response,
                               @ApiParam(required = true, name = "oldPassword", value = "旧密码") @RequestParam(value = "oldPassword", required = true) String oldPassword,
                               @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(required = true,value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("oldPassword", oldPassword);
            param.put("newPassword", newPassword);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.updatePassword(memberId,oldPassword, newPassword);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/updatePassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 管理版修改销售员状态
     * @param request
     * @param response
     * @param salesManId
     * @param status
     */
    @RequestMapping(value="/updateSalesManStatus", method= RequestMethod.POST)
    @ApiOperation(value="【管理版】冻结或者启用销售员。-1直接提示消息；")
    public void updateSalesManStatus(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required = true, name = "salesManId", value = "销售员Id") @RequestParam(value = "salesManId", required = true) Long salesManId,
                               @ApiParam(required = true, name = "status", value = "状态值：1代表冻结，0代表启用") @RequestParam(value = "status", required = true) Integer status,
                                     @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                     @ApiParam(required = true,value = "签名")@RequestParam(value = "sign", required = true) String sign     ){
        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("salesManId", salesManId);
            param.put("status", status);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            memberService.updateSalesManStatus(salesManId,status);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/updateSalesManStatus----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 代理商删除销售员
     * @param request
     * @param response
     * @param salesManId
     * @param agentId
     */
    @RequestMapping(value="/deleteSalesMan", method= RequestMethod.POST)
    @ApiOperation(value="【管理版】删除销售员。-1直接提示消息；")
    public void deleteSalesMan(HttpServletRequest request,
                                     HttpServletResponse response,
                                     @ApiParam(required = true, name = "salesManId", value = "销售员Id") @RequestParam(value = "salesManId", required = true) Long salesManId,
                                     @ApiParam(required = true, name = "agentId", value = "代理商id 即当前用户id") @RequestParam(value = "agentId", required = true) Long agentId,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(required = true,value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("salesManId", salesManId);
            param.put("agentId", agentId);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            memberService.deleteSalesMan(agentId,salesManId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/member/deleteSalesMan----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
